package com.ate.aoligei.db;

import android.util.Log;

import com.ate.aoligei.Student;
import com.ate.green.db.StudentDao;

import java.util.List;

/**
 * @author Ate
 * @description:
 * @date :2020/11/11 19:51
 */
public class DbHelper {
    public static boolean insertStudent(Student student){
        if (student==null){
            return false;
        }
        StudentDao studentDao = DbManager.getInstance().getDaoSession().getStudentDao();
        try {
            long l = studentDao.insertOrReplace(student);
            Log.e("添加成功","-------id:"+l);
            return true;
        }catch (Exception e){
            Log.e("添加失败","----");
            return false;
        }
    }
    public static List<Student> queryAll(){
        StudentDao studentDao = DbManager.getInstance().getDaoSession().getStudentDao();
        try {
            Log.e("","查询成功");
            return studentDao.loadAll();
        }catch (Exception e){
            Log.e("","查询失败");
            return null;
        }
    }
    public static boolean delete(){
        StudentDao studentDao = DbManager.getInstance().getDaoSession().getStudentDao();
        try {
            List<Student> students = studentDao.loadAll();
            studentDao.deleteByKey((long) (students.size()-1));
            Log.e("","删除成功");
            return true;
        }catch (Exception e){
            Log.e("","删除失败");
            return false;
        }

    }
    public static boolean update(Student student){
        StudentDao studentDao = DbManager.getInstance().getDaoSession().getStudentDao();
        try {
            studentDao.update(student);
            Log.e("","修改成功");
            return true;
        }catch (Exception e){
            Log.e("","修改失败");
            return false;
        }
    }
}
